﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Devices.Sensors;

namespace GameLogic.Controls
{
    public class AccelerometerHelper : UserControl, INotifyPropertyChanged
    {
        public AccelerometerHelper()
        {
            //if (DesignerProperties.IsInDesignTool)
            //{
            StackPanel sp = new StackPanel();
            sp.Background = new SolidColorBrush(Colors.Red);
            this.Content = sp;

            TextBlock tb = new TextBlock();
            tb.Text = "[ Accelerometer ]";
            sp.Children.Add(tb);
            
            tb = new TextBlock();
            tb.Text = "X:";
            sp.Children.Add(tb);

            _accelX = new TextBlock();
            sp.Children.Add(_accelX);

            tb = new TextBlock();
            tb.Text = "Y:";
            sp.Children.Add(tb);

            _accelY = new TextBlock();
            sp.Children.Add(_accelY);

            tb = new TextBlock();
            tb.Text = "Z:";
            sp.Children.Add(tb);

            _accelZ = new TextBlock();
            sp.Children.Add(_accelZ);
                
            //}

            if (!DesignerProperties.IsInDesignTool)
            {
                _acc = new Accelerometer();

                this.IsSensorEnabled = this.IsSensorEnabled;
            }
        }

        TextBlock _accelX;
        TextBlock _accelY;
        TextBlock _accelZ;

        //public double X_Acceleration
        //{
        //    get { return (double)GetValue(X_AccelerationProperty); }
        //    set { SetValue(X_AccelerationProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for X_Acceleration.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty X_AccelerationProperty =
        //    DependencyProperty.Register(
        //        "X_Acceleration", 
        //        typeof(double), 
        //        typeof(Accelerometer), 
        //        new PropertyMetadata(0.0, OnXAccelerationChanged));

        //private static void OnXAccelerationChanged(object sender, DependencyPropertyChangedEventArgs args)
        //{
        //    AccelerometerHelper acc = sender as AccelerometerHelper;
        //    acc._x = (double)args.NewValue;
        //}

        private double _initX, _initY, _initZ = 0;
        private double _x;

        public double X
        {
            get { return _x; }
            set { _x = value; }
        }
        private double _y;

        public double Y
        {
            get { return _y; }
            set { _y = value; }
        }
        private double _z;

        public double Z
        {
            get { return _z; }
            set { _z = value; }
        }

        private double _xSum;

        public double XSum
        {
            get { return _xSum; }
            set { _xSum = value; }
        }
        private double _ySum;

        public double YSum
        {
            get { return _ySum; }
            set { _ySum = value; }
        }
        private double _zSum;

        public double ZSum
        {
            get { return _zSum; }
            set { _zSum = value; }
        }

        Accelerometer _acc;
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
                PropertyChanged(this, args);
            }
        }

        private void SensorOn()
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }
            _acc.ReadingChanged += _acc_ReadingChanged;
            _acc.Start();
        }

        private void SensorOff()
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }
            _acc.Stop();
            _acc.ReadingChanged -= _acc_ReadingChanged;
        }

        public event EventHandler ValueChanged;
        private void OnValueChanged()
        {
            if (ValueChanged != null)
            {
                ValueChanged(this, EventArgs.Empty);
            }
        }

        void _acc_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            if (_accelX != null)
            {
                Dispatcher.BeginInvoke(() =>
                    {
                        _accelX.Text = e.X.ToString();
                        _accelY.Text = e.Y.ToString();
                        _accelZ.Text = e.Z.ToString();

                    });
            }
                
            if (_initX == 0)
            {
                _initX = e.X;
                _initY = e.Y;
                _initZ = e.Z;
            }

            _x = e.X - _initX;
            _y = e.X - _initY;
            _z = e.X - _initZ;

            _xSum += _x;
            _ySum += _y;
            _zSum += _z;

            
            OnValueChanged();

            //Y = e.Y;
            //Z = e.Z;
        }
        
        public bool IsSensorEnabled
        {
            get { return (bool)GetValue(IsSensorEnabledProperty); }
            set { SetValue(IsSensorEnabledProperty, value); }
        }

        public static readonly DependencyProperty IsSensorEnabledProperty =
            DependencyProperty.Register(
                "IsSensorEnabled", 
                typeof(bool), 
                typeof(AccelerometerHelper), 
                new PropertyMetadata(false, OnIsSensorEnabledChanged));

        private static void OnIsSensorEnabledChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }

            bool isEnabled = (bool)args.NewValue;
            AccelerometerHelper acc = sender as AccelerometerHelper;
            if (isEnabled)
            {
                acc.SensorOn();
            }
            else
            {
                acc.SensorOff();
            }
            
        }

        
    }
}
